/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#include <iostream>
#include "matmul_preload_custom_tiling.h"

namespace MatmulHost {

TCubeTiling GenerateTiling(const MatmulCaseParams& testCaseParams)
{
    TCubeTiling tilingData;
    auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance();
    matmul_tiling::MultiCoreMatmulTiling tiling(*ascendcPlatform);
    int32_t M = testCaseParams.m;
    int32_t N = testCaseParams.n;
    int32_t K = testCaseParams.k;
    int32_t blockDim = testCaseParams.usedCoreNum;
    bool isBias = testCaseParams.isBias;
    bool isAtrans = testCaseParams.isATrans;
    bool isBtrans = testCaseParams.isBTrans;

    tiling.SetDim(blockDim);
    tiling.SetAType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND,
        matmul_tiling::DataType::DT_FLOAT16, isAtrans);
    tiling.SetBType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND,
        matmul_tiling::DataType::DT_FLOAT16, isBtrans);
    tiling.SetCType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND,
        matmul_tiling::DataType::DT_FLOAT);
    tiling.SetBiasType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND,
        matmul_tiling::DataType::DT_FLOAT);
    tiling.SetOrgShape(M, N, K);
    tiling.SetShape(M, N, K);
    tiling.EnableBias(isBias);
    tiling.SetBufferSpace(-1, -1, -1);
#if defined(CUSTOM_PRELOAD_N)
    tiling.SetTraverse(matmul_tiling::MatrixTraverse::FIRSTN);
#else
    tiling.SetTraverse(matmul_tiling::MatrixTraverse::FIRSTM);
#endif

    if (tiling.GetTiling(tilingData) == -1) {
        std::cout << "Generate tiling failed." << std::endl;
        return {};
    }
    tilingData.baseM = testCaseParams.baseM;
    tilingData.baseN = testCaseParams.baseN;
    tilingData.baseK = testCaseParams.baseK;
    tilingData.stepM = testCaseParams.stepM;
    tilingData.stepN = testCaseParams.stepN;
    tilingData.stepKa = testCaseParams.stepKa;
    tilingData.stepKb = testCaseParams.stepKb;
    tilingData.depthA1 = testCaseParams.depthA1;
    tilingData.depthB1 = testCaseParams.depthB1;
    return tilingData;
}

} // namespace MatmulHost
